import * as base from "./base.js";
import instance from './request.js'
import { handleSuccessfulLogin } from './request.js'

window.addEventListener('load', function () {
    const $ = document.querySelector.bind(document);
    const $$ = document.querySelectorAll.bind(document);


    //用户名与密码
    $$('.loginbox input').forEach(function (value, index, array) {
        $$('.loginbox input')[index].addEventListener('focus', function () {
            this.parentNode.style.borderColor = '#008489';
        })
        $$('.loginbox input')[index].addEventListener('blur', function () {
            this.parentNode.style.borderColor = '#ebebeb';
        })
    })

    //房客与房东
    let home = 1;
    $$('.identity span input')[0].addEventListener('click', function () {
        home = 1;
    })
    $$('.identity span input')[1].addEventListener('click', function () {
        home = 2;
    })

    //表单
    let formnum1 = 1;
    let formnum2 = 1;
    $('.register').onsubmit = (e) => {
        formnum1 = 1;
        e.preventDefault();
        $$('.register .loginbox input').forEach(function (value, index, array) {
            if ($$('.register .loginbox input')[index].value === '') {
                formnum1 = 0;
            }
        })
        if (formnum1 === 1) {
            if ($('.register .agree input').checked === true) {
                axios({
                    method: 'POST',
                    url: 'http://43.139.230.42:8001/user/username',
                    data: {
                        "username": $$('.register .loginbox input')[0].value
                    }
                }).then(response => {
                    console.log(response.data);
                    if (response.data.code === 200) {
                        axios({
                            method: 'POST',
                            url: 'http://43.139.230.42:8001/enroll',
                            data: {
                                "username": $$('.register .loginbox input')[0].value,
                                "password": $$('.register .loginbox input')[1].value,
                                "identity": home
                            }
                        }).then(response => {
                            console.log(response);
                            console.log(home);
                            base.tooltipBox('', '注册成功!', '1');
                            $$('.forget')[0].click();
                        })
                    } else {
                        base.tooltipBox('', '该用户名已存在请直接登录', '0');
                    }
                })
            } else {
                base.tooltipBox('', '点击勾选方可进行注册登录', '0');
            }
        } else {
            base.tooltipBox('', '请完整输入用户名或密码', '0');
        }
    }
    $('.log').onsubmit = (e) => {
        formnum2 = 1;
        e.preventDefault();
        $$('.log .loginbox input').forEach(function (value, index, array) {
            if ($$('.log .loginbox input')[index].value === '') {
                formnum2 = 0;
            }
        })
        if (formnum2 === 1) {
            axios({
                method: 'POST',
                url: 'http://43.139.230.42:8001/login',
                data: {
                    "username": $$('.log .loginbox input')[0].value,
                    "password": $$('.log .loginbox input')[1].value,
                }
            }).then(response => {
                console.log(response);
                base.tooltipBox('', '登录成功!', '1');

                // 在用户成功登录后和每次用户操作后，调用相应的函数更新时间戳和记录用户操作
                handleSuccessfulLogin();
                

                localStorage.setItem('userid', response.data.data.userId);
                localStorage.setItem('username', $$('.log .loginbox input')[0].value);
                localStorage.setItem('password', $$('.log .loginbox input')[1].value);
                localStorage.setItem('identity', home);
                localStorage.setItem('token', response.data.data.token);
                location.href = 'index.html';
            })
        } else {
            base.tooltipBox('', '请完整输入用户名或密码', '0');
        }
    }

    //小眼睛
    let eyenum = 1;
    $$('.eyes').forEach(function (value, index, array) {
        $$('.eyes')[index].addEventListener('click', function () {
            if (eyenum === 1) {
                this.parentNode.children[0].type = 'text';
                this.parentNode.children[0].focus();
                this.children[0].style.display = 'none';
                this.children[1].style.display = 'block';
                eyenum = 0;
            } else {
                this.parentNode.children[0].type = 'password';
                this.parentNode.children[0].focus();
                this.children[0].style.display = 'block';
                this.children[1].style.display = 'none';
                eyenum = 1;
            }
        })
    })




    //注册与登录之间的转换
    let flag = 1;
    $$('.forget').forEach(function (value, index, array) {
        $$('.forget')[index].addEventListener('click', function () {
            if (flag === 1) {
                $('.register').style.display = 'none';
                $('.log').style.display = 'block';
                flag = 0;
            } else {
                $('.register').style.display = 'block';
                $('.log').style.display = 'none';
                flag = 1;
            }
        })
    })




})
